package org.example;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;

class StaffDAO extends BaseDAO<Staff> {
    public StaffDAO() {
        super(Staff.class);
    }

    @Override
    public void insert(Staff entity) throws Exception {
        if (entity == null) {
            throw new IllegalArgumentException("员工实体不能为空");
        }
        entity.setCreate_time(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        super.insert(entity);
    }

    public void insert(Scanner scanner) throws Exception {
        if (scanner == null) {
            throw new IllegalArgumentException("Scanner对象不能为空");
        }

        System.out.println("==zqw=== 请输入员工信息 ==zqw===");
        Staff staff = new Staff();

        System.out.print("请输入卡号：");
        staff.setCard_id(scanner.nextLine().trim());

        System.out.print("请输入姓名：");
        staff.setName(scanner.nextLine().trim());

        while (true) {
            System.out.print("请输入性别（男/女）：");
            String gender = scanner.nextLine().trim();
            if ("男".equals(gender) || "女".equals(gender)) {
                staff.setGender(gender);
                break;
            } else {
                System.out.println("性别输入有误，请重新输入（男/女）");
            }
        }

        while (true) {
            try {
                System.out.print("请输入年龄：");
                int age = Integer.parseInt(scanner.nextLine().trim());
                if (age > 0 && age < 150) {
                    staff.setAge(age);
                    break;
                } else {
                    System.out.println("年龄必须在1-149之间");
                }
            } catch (NumberFormatException e) {
                System.out.println("年龄输入有误，请输入数字");
            }
        }

        while (true) {
            System.out.print("请输入身份证号：");
            String idNumber = scanner.nextLine().trim();
            if (idNumber.length() == 18) {
                staff.setId_number(idNumber);
                break;
            } else {
                System.out.println("身份证号必须是18位");
            }
        }

        this.insert(staff);
        System.out.println("员工信息插入成功");
    }
}
